package newApi;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

public class StreamTest1 {
    public static void main(String[] args) {
        // 流的常用创建方式
        Stream stream1 = Stream.of("beijing", "tianjing", "shanghai");
        stream1.forEach(System.out::println);

        String[] myArray = new String[]{"hello", "world"};
        Stream stream2 = Stream.of(myArray);
        Stream stream3 = Arrays.stream(myArray);

        Stream stream4 = Arrays.asList(myArray).stream();

        // 基本使用
        IntStream.of(new int[]{5, 6, 7}).forEach(System.out::println);
        System.out.println("----[3,8)--------");
        IntStream.range(3, 8).forEach(System.out::println);
        System.out.println("-----[3,8]-------");
        IntStream.rangeClosed(3, 8).forEach(System.out::println);

        System.out.println("------统计源数据里单元个数------");
        long count = IntStream.of(new int[]{1, 2, 3}).count();
        System.out.println(count);
        System.out.println("------1、求和------");
        long sum = IntStream.of(new int[]{1, 2, 3}).map(value -> 2 * value).sum();
        System.out.println(sum);
        System.out.println("------2、求和------");
        long sum2 = IntStream.of(new int[]{1, 2, 3}).map(value -> 2 * value).reduce(0, Integer::sum);
        System.out.println(sum2);


        List<Person> persons = new ArrayList<Person>() {
            {
                add(new Person(20, "maomao"));
                add(new Person(30, "mayun"));
                add(new Person(26, "keke"));
                add(new Person(40, "angular"));
            }
        };
        /**
         * stream.toArray将一个流转换为当前流内元素类型的数组
         * 其中toArray参数函数式接口IntFunction的构造函数第一个参数为当前流元素的个数
         */
        // 将一个流转换为数组
        Stream<Person> streamArr = persons.stream();
        System.out.println("------将一个流转换为数组-----");
        Person[] people = streamArr.toArray(Person[]::new);
        for (Person person : people) {
            System.out.println(person.getName());
        }
        String[] strings = Stream.of("welcome", "to", "beijing").toArray(length -> new String[length]);

        /**
         * stream.collect
         */
        Stream<Person> streamCol = persons.stream();
        System.out.println("------将一个流转换为集合-----");
        List<Person> personListCol = streamCol.collect(Collectors.toList());
        personListCol.forEach((person) -> System.out.println(person.getName()));

        System.out.println("------使用serial stream测试collect方法的第三个参数-----");
        Stream<Person> streamSerial = persons.stream();
        List<Person> personList1 = streamSerial.collect(() -> new ArrayList(), (theList, item) -> theList.add(item), (theList1, theList2) -> {
            System.out.println(theList1); // 根本不进入断点
            System.out.println(theList2);
            theList1.addAll(theList2);
        });
        personList1.forEach(person -> System.out.println(person.getName()));
        System.out.println(personList1.size());
        System.out.println("------使用parallel stream测试collect方法的第三个参数-----");
        Stream<Person> streamParallel = persons.parallelStream();
        List<Person> personList2 = streamParallel.collect(() -> new ArrayList(), (theList, item) -> theList.add(item), (theList1, theList2) -> {
            System.out.println(theList1); // 进入断点
            System.out.println(theList2);
            theList1.addAll(theList2);
        });
        personList2.forEach(person -> System.out.println(person.getName()));

        // 自己手动封装LinkedList
        streamParallel.collect(LinkedList::new, LinkedList::add, LinkedList::addAll);
        streamParallel.collect(Collectors.toCollection(LinkedList::new));


    }
}
